﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace TinyCinema.Data
{
    public class Country : Execution
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public ObservableCollection<Movie> Movies { get; set; }
        public static ObservableCollection<Country> lstCountry { get; set; }
        int _Index { get; set; }
        public int Index
        {
            get { return _Index; }
            set
            {                
                ID = lstCountry[value].ID;
                Name = lstCountry[value].Name;
                _Index = value;                
            }
        }
        public Country()
        {
            if (lstCountry == null)
            {
                lstCountry = new ObservableCollection<Country>();
                lstCountry = getAllCountry();
            }
            Index = 0;
        }
        public Country(string _ID)
        {
            PutTableIntoDataSet(Tables.Country);
            var tbl = ds.Tables[Tables.Country];
            var Row = tbl.Select(CountryTable.ID + "='" + _ID + "'")[0];
            ID = Row[CountryTable.ID] as String;
            Name = Row[CountryTable.Name] as String;

            if (lstCountry == null)
            {
                lstCountry = new ObservableCollection<Country>();
                lstCountry = getAllCountry();
            }
            for (int i = 0; i < lstCountry.Count; ++i)
                if (ID == lstCountry[i].ID) Index = i;
            
        }
        static ObservableCollection<Country> getAllCountry()
        {
            var lst = new ObservableCollection<Country>();
            PutTableIntoDataSet(Tables.Country);
            var tbl = ds.Tables[Tables.Country];
            var Rows = tbl.Select();
            lst = new ObservableCollection<Country>();
            foreach (var row in Rows)
                lst.Add(new Country(row[CountryTable.ID] as String));
            return lst;
        }
    }
}
